package com.example.demo.service;

import com.example.demo.entity.LogEntity;
import com.example.demo.entity.UserEntity;
import com.example.demo.mapper.LogMapper;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: DELL
 * Date: 2023-06-04
 * Time: 17:11
 */
@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LogService logService;

    public int del(Integer id) throws InterruptedException {
        System.out.println("sql开始执行.");
        TimeUnit.SECONDS.sleep(5);
        System.out.println("等待5秒.");
        int result = userMapper.del(id);
        System.out.println("sql执行完成.");
        System.out.println("所有资源都已释放.");
        return result;
    }

    @Transactional(propagation = Propagation.NESTED)
    public int add(UserEntity user) {
        // 添加用户
        int addListUserResult = userMapper.add(user);
        System.out.println("添加用户条数 : " + addListUserResult);
        // 添加日志信息
        LogEntity log = new LogEntity();
        log.setMessage("添加用户成功");
        logService.add(log);
        return addListUserResult;
    }
}
